package baiqitun.netfiles.service.impl;

import baiqitun.netfiles.entity.BaseEntity;
import baiqitun.netfiles.entity.SysMapping;
import baiqitun.netfiles.service.MappingService;
import baiqitun.netfiles.service.SysMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MappingServiceImpl<T extends BaseEntity> extends EntityServiceImpl<T> implements MappingService<T> {
    @Autowired
    private SysMappingService sysMappingService;

    @Override
    public void save(String path, T entity) {
        super.save(entity);

        SysMapping mapping = new SysMapping();
        mapping.setPath(path);
        mapping.setId(entity.getId());

        sysMappingService.save(mapping);
    }

    @Override
    public String getMappingPath(String id) {
        SysMapping mapping = sysMappingService.get(id);
        return mapping.getPath();
    }

    @Override
    public T getByPath(String path) {
        SysMapping mapping = sysMappingService.get(path);
        if (mapping == null){
            return null;
        }
        return get(mapping.getId());
    }

}
